package com.pagemodel.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value={"com/pagemodel/login"})
public class Login  {


	@RequestMapping(value = {"/Login"}, method = RequestMethod.POST)
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setHeader("content-type","text/html;charset=UTF-8");//防止乱码
		response.setCharacterEncoding("UTF-8"); //防止乱码

		String  user =request.getParameter("user"); //getParameter对应的是jsp中的name值
		String password = request.getParameter("password"); //getParameter对应的是jsp中的name值
		

		request.setAttribute("user", user);//提交不要清空值
		//request.setAttribute("password", password);
		String forward;  
		if(user==null||user.trim().isEmpty()) {
//			forward="error.jsp";  
//			RequestDispatcher rd=request.getRequestDispatcher(forward);  
//	        rd.forward(request,response); 
		}
		if(password==null||password.trim().isEmpty()) {
//			forward="error.jsp";  
//			RequestDispatcher rd=request.getRequestDispatcher(forward);  
//	        rd.forward(request,response); 
		}
		//ServletConfig config= getServletConfig();  //在XML中添加赋值，通过getServletConfig()获取到对于字段的值
		String id= "ganhx";// config.getInitParameter("user");
		String pw= "ganhx";// config.getInitParameter("password");
		
		if(!id.equals(user)) {
			response.getWriter().print("该用户名未注册！");
		}
		
		if(id.equals(user)&&!pw.equals(password)) {
			response.getWriter().print("该用户名密码不正确！");
		}
		
		List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("user", user);
		map.put("password", password);
		list.add(map);
		response.getWriter().print(JSON.toJSONString(list));

//		if(id.equals(user)&&pw.equals(password)) {//用户名账号密码正确跳转
//			forward="com/page/main/StudentMain.jsp";
//
//
//
//			HttpSession session =request.getSession();
//            session.setAttribute("currentUser", id);
//            request.getRequestDispatcher(forward).forward(request,response);//服务端跳转
//            //response.sendRedirect(forward); //客户端跳转
//		}

	}


}
